package steak.xteamy.client;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import steak.xteamy.KillerGroup;
import steak.xteamy.Network.Packets;
import steak.xteamy.client.VoiceChat.Network;

import java.lang.reflect.Constructor;

public class KillerGroupClient implements ClientModInitializer {

    public static ChannelButtonState channelButtonState = new ChannelButtonState(true, false);
    public static int channelP = -1;

    public static KeyBinding KEY_WOLF_CHANNEL = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.killergroup.wolf",
            InputUtil.Type.KEYSYM,
            -1,
            "category.killergroup"
    ));
    public static KeyBinding KEY_VOICE_CHAT = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.killergroup.voice_chat_gui",
            InputUtil.Type.KEYSYM,
            -1,
            "category.killergroup"
    ));

    @Override
    public void onInitializeClient() {
        Network.networkHandler();

        ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> setChannelP(-1));

        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            while (KEY_WOLF_CHANNEL.wasPressed()) {
                if (channelP == 1) {
                    if (!channelButtonState.getBoolean(1)) {
                        channelButtonState.reset();
                        channelButtonState.setBoolean(1, true);
                        ClientPlayNetworking.send(new Packets.Channel(1));
                    } else {
                        channelButtonState.reset();
                        channelButtonState.setBoolean(0, true);
                        ClientPlayNetworking.send(new Packets.Channel(0));
                    }
                }
            }

            if (KEY_VOICE_CHAT.isPressed()) {
                try {
                    Class<?> voiceChatSettingsScreenClass = Class.forName("de.maxhenkel.voicechat.gui.VoiceChatScreen");
                    Constructor<?> constructor = voiceChatSettingsScreenClass.getConstructor();

                    Screen voiceChatSettingsScreen = (Screen) constructor.newInstance();
                    client.execute(() -> client.setScreen(voiceChatSettingsScreen));
                } catch (Exception e) {
                    KillerGroup.LOGGER.warn("Failed to set voice chat settings", e);
                }
            }
        });
    }

    public static void setChannelP (int channelP) {
        if (channelP == 1 && channelButtonState.getBoolean(1)) {
            return;
        }
        channelButtonState.reset();
        KillerGroupClient.channelP = channelP;
        channelButtonState.setBoolean(0, true);
    }
}
